﻿#include <learn/topo/topo_builder.h>
#include <learn/topo/topo_coedge.h>
#include <learn/topo/topo_edge.h>
#include <learn/topo/topo_vertex.h>

using namespace xi;
using namespace xi::topo;

Topo_Edge::Topo_Edge() : Topo_Shape(), m_start(nullptr), m_end(nullptr), m_coedge(nullptr), m_dir(Topo_Dir::Forward)
{
}

Topo_Edge::~Topo_Edge()
{
    Topo_Builder builder;

    // 解除连接，删除 coedge（coedge 会删除 twin）
    if (m_coedge != nullptr)
    {
        m_coedge->edge() = nullptr;
        m_coedge->twin()->edge() = nullptr;
        builder.remove_coedge(m_coedge);
        m_coedge = nullptr;
    }

    std::erase(m_start->edges(), this);
    std::erase(m_end->edges(), this);
}

Topo_Coedge *&Topo_Edge::coedge()
{
    return m_coedge;
}

Topo_Vertex *&Topo_Edge::start()
{
    return m_start;
}

Topo_Vertex *&Topo_Edge::end()
{
    return m_end;
}

Topo_Dir Topo_Edge::dir() const
{
    return m_dir;
}